﻿using MiniExcelLibs;
using Mysqlx.Crud;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 完工订单定时任务.http;
using 完工订单定时任务.models.sqlservermodel;

namespace 完工订单定时任务.报表
{
    public partial class Form材料出库推送记录 : Form
    {
        public Form材料出库推送记录()
        {
            InitializeComponent();
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button查询_Click(object sender, EventArgs e)
        {
            string url = "http://115.231.66.30:23001/ViewReport/NCMaterialOut";
            Dictionary<string,string> dic = new Dictionary<string,string>();
            dic.Add("单据日期开始", dateTimePicker单据日期开始.Value.ToString("yyyy-MM-dd"));
            dic.Add("单据日期结束", dateTimePicker单据日期结束.Value.AddDays(1).ToString("yyyy-MM-dd"));
            dic.Add("工单号", textBox工单号.Text);

            DataTable dt = HttpHelper.HttpPost<DataTable>(url, JsonConvert.SerializeObject(dic));
            dataGridView1.DataSource = dt;
            dataGridView1.Refresh();
        }

        private void 查询()
        {
            string sql = $@"select mm_pmo.dbilldate 订单日期,mm_pmo.vbillcode 工单号,
                            CASE
                            WHEN mm_mo.cdeptid LIKE '1001A110000000V585T3' THEN '1'
                            WHEN mm_mo.cdeptid LIKE '1001A110000000V58DLM' THEN '2'
                            ELSE mm_mo.cdeptid
                            END AS 产线, materialtype AS 产品型号
                            from mm_pmo  join mm_mo on  mm_mo.cpmohid = mm_pmo.CPMOHID 
                            left join bd_material on bd_material.pk_material = mm_mo.cmaterialid
                            where mm_mo.dr=0  and mm_pmo.pk_org = '0001A11000000000Q5NJ' ";
            if (dateTimePicker单据日期开始.Value != null)
            {
                sql += $"and mm_pmo.dbilldate >= '{dateTimePicker单据日期开始.Value.ToString("yyyy-MM-dd")}'";
            }
            if (dateTimePicker单据日期结束.Value != null)
            {
                sql += $"and mm_pmo.dbilldate <= '{dateTimePicker单据日期结束.Value.ToString("yyyy-MM-dd")}'";
            }
            if (!string.IsNullOrEmpty(textBox工单号.Text))
            {
                sql += $"and mm_pmo.vbillcode like '%{textBox工单号.Text}%'";
            }

            List<VIEW_拉伸报表> vIEW_s = OraSqlSugarHelp.scope.Ado.SqlQuery<VIEW_拉伸报表>(sql);
            if (vIEW_s != null && vIEW_s.Count > 0)
            {
                List<string> 工单s = vIEW_s.Select(x => x.工单号).ToList();
                string inStr = string.Join("','", 工单s);
                sql = $"select * from view_材料出库推送记录 where 工单号 in ('{inStr}')";
                DataTable dt = SqlSugarHelp.scope.Ado.GetDataTable(sql);
                dataGridView1.DataSource = dt;
                dataGridView1.Refresh();
            }
            else
            {
                MessageBox.Show("没有查询到数据");
            }

        }

        private void button导出_Click(object sender, EventArgs e)
        {
            //导出excel
            if (dataGridView1.DataSource != null)
            {
                DataTable dt = (DataTable)dataGridView1.DataSource;
                string fileName = "材料出库推送记录" + DateTime.Now.ToFileTime() + ".xlsx";
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "All files (*.*)|*.*";
                saveFileDialog.FileName = fileName;
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    MiniExcel.SaveAs(saveFileDialog.FileName,dt);
                    MessageBox.Show("导出成功");
                }
                
            }
            else
            {
                MessageBox.Show("没有数据");
            }
        }

        private void Form材料出库推送记录_Load(object sender, EventArgs e)
        {
            dateTimePicker单据日期开始.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        }
    }
}
